/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * StudentDetails.java
 *
 * Created on 11-10-2011, 09:56:22
 */
package gui;

import buz.StudentBuz;
import dto.StudentDTO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.midi.SysexMessage;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author XMANI
 */
public class StudentDetails extends javax.swing.JFrame {

    StudentBuz sb = new StudentBuz();
    Locale locale;

    /** Creates new form StudentDetails */
    public StudentDetails() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
        }

        locale = new Locale("en", "US");
        initComponents();
        Vector header = new Vector();
        header.add("Ten");
        header.add("Dia chi");
        header.add("Ngay sinh");
        DefaultTableModel model = new DefaultTableModel(header, 0);
        ArrayList<StudentDTO> list = new ArrayList<StudentDTO>();
        list = sb.getStudent();
        for (StudentDTO stu : list) {
            Vector v = new Vector();
            v.add(stu.getName());
            v.add(stu.getAddress());
            v.add(stu.getBirthday());
            model.addRow(v);
        }
        tblStudent.setModel(model);
        sb.encrypt();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnExit = new javax.swing.JButton();
        scpEn = new javax.swing.JScrollPane();
        txaEn = new javax.swing.JTextArea();
        scpDe = new javax.swing.JScrollPane();
        txaDe = new javax.swing.JTextArea();
        btnSave = new javax.swing.JButton();
        btnOpen = new javax.swing.JButton();
        scptblStudent = new javax.swing.JScrollPane();
        tblStudent = new javax.swing.JTable();
        btnEN = new javax.swing.JButton();
        btnVn = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Information");

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        txaEn.setColumns(20);
        txaEn.setRows(5);
        scpEn.setViewportView(txaEn);

        txaDe.setColumns(20);
        txaDe.setRows(5);
        scpDe.setViewportView(txaDe);

        btnSave.setText("Save");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnOpen.setText("Open");
        btnOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOpenActionPerformed(evt);
            }
        });

        scptblStudent.setViewportView(tblStudent);

        btnEN.setText("EN");
        btnEN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnENActionPerformed(evt);
            }
        });

        btnVn.setText("VN");
        btnVn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(scpDe, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnEN)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnVn)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnSave)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnOpen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnExit))
                    .addComponent(scptblStudent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(scpEn, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(scptblStudent, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(scpEn, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(13, 13, 13)
                .addComponent(scpDe, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit)
                    .addComponent(btnSave)
                    .addComponent(btnOpen)
                    .addComponent(btnEN)
                    .addComponent(btnVn))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-488)/2, (screenSize.height-510)/2, 488, 510);
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        System.exit(0);

    }//GEN-LAST:event_btnExitActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        FileInputStream in = null;
        try {
            // TODO add your handling code here:
            sb.encrypt();
            in = new FileInputStream("encrypt.txt");
            byte[] encryptText = new byte[in.available()];
            in.read(encryptText);
            String a = new String(encryptText);
            txaEn.setText(a);
        } catch (IOException ex) {
            Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_btnSaveActionPerformed

    private void btnOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOpenActionPerformed
        FileInputStream in = null;
        try {
            // TODO add your handling code here:
            sb.decriypt();
            in = new FileInputStream("decrypt.txt");
            byte[] decryptText = new byte[in.available()];
            in.read(decryptText);
            String a = new String(decryptText);
            txaDe.setText(a);
        } catch (IOException ex) {
            Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(StudentDetails.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }//GEN-LAST:event_btnOpenActionPerformed

    private void btnENActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnENActionPerformed
        // TODO add your handling code here:
        locale = new Locale("en", "US");
        ResourceBundle bundle = ResourceBundle.getBundle("buz/traslate", locale);
        btnOpen.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("OPEN"));
        btnSave.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("SAVE"));
        btnExit.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("EXIT"));
        btnEN.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("EN"));
        btnVn.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("VI"));
        setTitle(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("INFORMATION"));

    }//GEN-LAST:event_btnENActionPerformed

    private void btnVnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVnActionPerformed
        // TODO add your handling code here:
        locale = new Locale("vi", "VN");
        ResourceBundle bundle = ResourceBundle.getBundle("buz/traslate", locale);
        btnOpen.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("OPEN"));
        btnSave.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("SAVE"));
        btnExit.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("EXIT"));
        btnEN.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("EN"));
        btnVn.setText(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("VI"));
        setTitle(java.util.ResourceBundle.getBundle("buz/traslate",locale).getString("INFORMATION"));
    }//GEN-LAST:event_btnVnActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new StudentDetails().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnEN;
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnOpen;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnVn;
    private javax.swing.JScrollPane scpDe;
    private javax.swing.JScrollPane scpEn;
    private javax.swing.JScrollPane scptblStudent;
    private javax.swing.JTable tblStudent;
    private javax.swing.JTextArea txaDe;
    private javax.swing.JTextArea txaEn;
    // End of variables declaration//GEN-END:variables
}
